﻿
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Windows;

namespace HttpLoadBalancer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            cbSortOrder.Items.Add("Descending Response TotalProcessorTime");
            cbSortOrder.Items.Add("Ascending Response TotalProcessorTime");
            cbSortOrder.SelectedIndex = 0;
            tbUrl.Text = ConfigurationManager.AppSettings["urls"].Replace(";","\r");
        }

        StringBuilder stringBuilder;
        List<ProcessInfo> processes = new List<ProcessInfo>();
        private void button_Click(object sender, RoutedEventArgs e)
        {
            stringBuilder = new StringBuilder();
            int noOfThreads = int.Parse( tbNoOfThreads.Text );
           
            List<string> urls = tbUrl.Text.Split('\r').ToList();
            
            for (int i = 0; i < noOfThreads; i++)
            {
                var random = new Random();
                int idx = random.Next (0,urls.Count);
               var processInfo = new ProcessInfo("UrlCaller.exe", urls[idx]);
               processes.Add(processInfo);
               processInfo.Start();
            }

            btnCheckResult_Click(sender, e);


        }

        private void btnCheckResult_Click(object sender, RoutedEventArgs e)
        {
            stringBuilder = new StringBuilder();
            int i = 0;
            stringBuilder.Append( "\r\nProcesses...");
            List<ProcessInfo> processList;
            if (cbSortOrder.SelectedIndex == 0)
            {
                processList = processes.OrderByDescending(s => s.TotalProcessorTime).ToList();
            }
            else// if(cbSortOrder.SelectedIndex == 1)
            {
                processList = processes.OrderBy(s => s.TotalProcessorTime).ToList();
            } 
            foreach (var process in processList)
            {
                string line = "";
                if (!process.HasExited && !process.Process.HasExited  )
                {
                    line=string.Format
                        (
                            "\r\nprocess {0} is alive and has taken {1} ms. Errors:{2}", 
                            process.Url, 
                            process.TotalProcessorTime.Milliseconds,
                            process.ErrorInfo
                        );
                }
                else
                {
                    line=string.Format
                        (
                            "\r\nprocess {0} is finished and took {1} ms. Errors:{2}", 
                            process.Url, 
                            process.TotalProcessorTime.Milliseconds, 
                            process.ErrorInfo
                        );
                }
                stringBuilder.Append(line);
                i++;
            }
            tbResult.Text = stringBuilder.ToString();
        }
    }
}
